/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.bootstrap;


import org.apache.catalina.connector.Connector;
import org.mybatis.spring.annotation.MapperScan;
import org.openislands.oi.config.PackageFilter;
import org.openislands.oi.config.YamlPropertySourceFactory;
import org.openislands.oi.constant.Dict;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;


@EnableScheduling
@SpringBootApplication
@MapperScan({"org.openislands.oi.dao.mapper", "com.gitee.sunchenbin.mybatis.actable.dao"})
@PropertySource(factory = YamlPropertySourceFactory.class,
        value = {"classpath:" + Dict.COMMON_CONFIG_FILE, "classpath:" + Dict.MASTER_CONFIG_FILE}, name = Dict.MASTER_MARK)
@ComponentScan(basePackages = {"org.openislands.oi"},
        excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = PackageFilter.Master.class))
public class Master implements WebMvcConfigurer, TomcatConnectorCustomizer {
    public static void main(String[] args) {
        try {
            Bootstrap.start(Master.class, args);
        } catch (Exception e) {
            Bootstrap.log.error(e.getMessage(), e);
        }
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

    @Override
    public void customize(Connector connector) {
        Bootstrap.setConnector(connector);
    }
}
